package com.google.springfacestools;

import javax.faces.application.NavigationHandler;
import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.springfacestools.validation.ValidationMessages;

@Component("jsfNavigationHandler")
public class SpringNavigationHandler extends NavigationHandler {

	@Autowired
	private ValidationMessages errors;
	
	@Override
	public void handleNavigation(FacesContext context, String fromAction, String outcome) {
		if(!errors.hasErrors()) {
			ViewHandler viewHandler = context.getApplication().getViewHandler();
			UIViewRoot viewRoot = viewHandler.createView(context, outcome);
			context.setViewRoot(viewRoot);
			context.renderResponse();
		}
	}

}
